<?php

define('APP_KEY', '32602393');
define('APP_SECRET', 'd631bb90a1d6f054ffc00a20abf6eaee');

/* 没有该权限，改用大淘客的
function order_details($startTime, $endTime, $pageNo = 1, $positionIndex = '', $pageSize = 100, $orderScene = 1)
{
    vendor("taobao.TopSdk");
    $c = new \TopClient;
    $c->appkey = APP_KEY;
    $c->secretKey = APP_SECRET;
    $c->format = 'json';

    $orders = [];
    $req = new TbkOrderDetailsGetRequest;

    $req->setQueryType("2");//查询时间类型，1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询
    if ($positionIndex) $req->setPositionIndex($positionIndex);//位点，除第一页之外，都需要传递；前端原样返回。
    $req->setPageSize($pageSize);//	页大小，默认20，1~100
//    $req->setMemberType("2");//推广者角色类型,2:二方，3:三方，不传，表示所有角色
//    $req->setTkStatus("12");//淘客订单状态，12-付款，13-关闭，14-确认收货，3-结算成功;不传，表示所有状态
    $req->setEndTime($endTime);//订单查询结束时间，订单开始时间至订单结束时间，中间时间段日常要求不超过3个小时，但如618、双11、年货节等大促期间预估时间段不可超过20分钟，超过会提示错误，调用时请务必注意时间段的选择，以保证亲能正常调用！
    $req->setStartTime($startTime);//订单查询开始时间
    $req->setJumpType("1");//	跳转类型，当向前或者向后翻页必须提供,-1: 向前翻页,1：向后翻页
    $req->setPageNo($pageNo);//第几页，默认1，1~100
    $req->setOrderScene($orderScene);//场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1
    $resp = $c->execute($req);
    return json_decode(json_encode($resp), true);
}
*/

function curlHelper($url, $data = null, $header = [], $cookies = "", $method = 'GET')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_COOKIE, $cookies);
    switch ($method) {
        case  "GET":
            curl_setopt($ch, CURLOPT_HTTPGET, true);
            break;
        case  "POST":
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case  "PUT":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case  "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case  "PATCH":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case  "TRACE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "TRACE");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case  "OPTIONS":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "OPTIONS");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        case  "HEAD":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            break;
        default :
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $response = curl_exec($ch);
    $output = [];
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    // 根据头大小去获取头信息内容
    $output['header'] = substr($response, 0, $headerSize);
    $output['body'] = substr($response, $headerSize, strlen($response) - $headerSize);
    $output['detail'] = curl_getinfo($ch);
    curl_close($ch);
    return $output;
}
